package com.jfinal.aceadmin.util;

import java.util.Date;

import com.jfinal.aceadmin.model.ChatLog;
import com.jfinal.aceadmin.model.Email;
import com.jfinal.aceadmin.model.EmailAttachment;
import com.jfinal.aceadmin.model.User;
import com.jfinal.aceadmin.model.UserMessage;
import com.jfinal.aceadmin.model.UserNotification;
import com.jfinal.aceadmin.model.UserTask;
import com.jfinal.aceadmin.vo.AttachmentVo;
import com.jfinal.aceadmin.vo.ChatLogVo;
import com.jfinal.aceadmin.vo.EmailSimpleVo;
import com.jfinal.aceadmin.vo.MessageVo;
import com.jfinal.aceadmin.vo.NotificationVo;
import com.jfinal.aceadmin.vo.TaskVo;

public class VoUtil {

	public static ChatLogVo convert(ChatLog chatLog) {
		if (null != chatLog) {
			ChatLogVo chatLogVo = new ChatLogVo();
			chatLogVo.setContent(chatLog.getContent());
			chatLogVo.setUserId(chatLog.getUserId());
			chatLogVo.setUsername(chatLog.getUsername());
			int userId = chatLog.getUserId();
			User user = User.dao.findById(userId);
			String icon = user.getIcon();
			String realIcon = PropertiesUtil.getIconServer() + icon;
			chatLogVo.setUserIcon(realIcon);
			long recentTime = DateUtil.getMistiming(chatLog.getCreatedDate());
			chatLogVo.setRecentTime(recentTime);
			return chatLogVo;
		}
		return null;
	}

	public static TaskVo convert(UserTask uTask) {
		if (null != uTask) {
			TaskVo taskVo = new TaskVo();
			taskVo.setDescription(uTask.getDescription());
			taskVo.setRate(uTask.getRate());
			return taskVo;
		}
		return null;
	}

	public static NotificationVo convert(UserNotification uNotification) {
		if (null != uNotification) {
			NotificationVo notificationVo = new NotificationVo();
			notificationVo.setDescription(uNotification.getDescription());
			notificationVo.setQuantity(uNotification.getQuantity());
			notificationVo.setType(uNotification.getType());
			return notificationVo;
		}
		return null;
	}

	public static MessageVo convert(UserMessage uMessage) {
		if (null != uMessage) {
			MessageVo messageVo = new MessageVo();
			messageVo.setContent(uMessage.getContent());
			messageVo.setSenderName(uMessage.getSenderName());
			String showTimeString = StringUtil.getTimeTemplate(uMessage
					.getCreatedDate());
			messageVo.setRecentTime(showTimeString);
			int userId = uMessage.getSenderId();
			User user = User.dao.findById(userId);
			String icon = user.getIcon();
			String realIcon = PropertiesUtil.getIconServer() + icon;
			messageVo.setSenderIcon(realIcon);
			return messageVo;
		}
		return null;
	}

	public static EmailSimpleVo convert(Email email) {
		if (null != email) {
			EmailSimpleVo emailSimpleVo = new EmailSimpleVo();
			long emailId = email.getBaseId();
			emailSimpleVo.setEmailId(emailId);
			emailSimpleVo.setFlaged(email.getFlaged());
			emailSimpleVo.setReaded(email.getReaded());
			emailSimpleVo.setSenderName(email.getSenderName());
			emailSimpleVo.setSubject(email.getSubject());
			int userId = email.getSenderId();
			User user = User.dao.findById(userId);
			String icon = user.getIcon();
			String realIcon = PropertiesUtil.getIconServer() + icon;
			Date senderDate = email.getCreatedDate();
			emailSimpleVo.setSendTime(DateUtil.getAmPmDate(senderDate));
			emailSimpleVo.setSenderIcon(realIcon);
			return emailSimpleVo;
		}
		return null;
	}

	public static AttachmentVo convert(EmailAttachment attachment) {
		if (null != attachment) {
			AttachmentVo attachmentVo = new AttachmentVo();
			attachmentVo.setFileName(attachment.getFileName());
			String filePath = attachment.getFilePath();
			String realPath = PropertiesUtil.getAttachmentServer() + filePath;
			attachmentVo.setFilePath(realPath);
			attachmentVo.setFileSize(attachment.getFileSize());
			return attachmentVo;
		}
		return null;
	}

}
